//
//  SEGWinningPublicViewController.m
//  SEGUhomeStandard
//
//  Created by 揭婧雯 on 2018/4/18.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGWinningPublicViewController.h"

#import "SEGWinningPublicTopView.h"
#import "SEGWinningPublicDetailView.h"
#import "SEGMessageNaviTitleNomalView.h"

#import "SEGWinningPublicViewModel.h"

@interface SEGWinningPublicViewController () <SEGMessageNaviTitleNomalViewDelegate, UIScrollViewDelegate, SEGprotocolBottomToolViewDelegate, SEGprotocolWinningPublicDetailViewDelegate>

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) SEGMessageNaviTitleNomalView *naviTitle;
/// 主视图
@property (nonatomic, strong)UIScrollView *scrollView;
/// 头部视图
@property (nonatomic, strong)SEGWinningPublicTopView *topView;
/// 详情视图
@property (nonatomic, strong)SEGWinningPublicDetailView *detailView;

/// viewModel
@property (nonatomic, strong) SEGWinningPublicViewModel *viewModel;

@end

static CGFloat bottomViewHeight = 44.0;

@implementation SEGWinningPublicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_W;
    self.navDisplayType = SEGNavigationBarDisplayTypeHide;
    
    [self seg_setupNavigationView];
    
    [self requestWinningPublicDetail];
}

#pragma mark - UI
- (void)seg_setupNavigationView {
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.naviTitle];
    [self.naviTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(kNavigationBarAndStatusBarHeight);
    }];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        if ( LYIsiPhoneX() ) { //适配iPhoneX 下移15
            make.top.equalTo(self.view).offset(45);
        }else {
            make.top.equalTo(self.view).offset(30);
        }
    }];
}

#pragma mark - Load Data
- (void)requestWinningPublicDetail {
    @weakify(self);
    [SVProgressHUD show];
    [[self.viewModel.command execute:self.noticeId] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [SVProgressHUD dismiss];
        self.naviTitle.contentLabel.text = self.viewModel.winningPublicModel.noticeName;
        
        // 头部视图
        // 控件数据模型创建
        SEGWinningPublicTopViewModel *data = [[SEGWinningPublicTopViewModel alloc] init];
        data.title = self.viewModel.winningPublicModel.noticeName;
        data.bannerUrlString = self.viewModel.winningPublicModel.noticeBanner;
        // 控件内容布局计算
        SEGWinningPublicTopViewLayout *layout = [[SEGWinningPublicTopViewLayout alloc] initWithData:data type:SEGEnumWinningPublicTopViewType_NO];
        // 内容布局填充到视图
        SEGWinningPublicTopView *varWinningPublicTopView = [[SEGWinningPublicTopView alloc]initWithFrame:CGRectZero];
        varWinningPublicTopView.left = 0;
        varWinningPublicTopView.top = 0;
        [varWinningPublicTopView setWithLayout:layout];
        self.topView = varWinningPublicTopView;
        [self.scrollView addSubview:varWinningPublicTopView];
        
        //描述
        // 控件数据模型创建
        SEGWinningPublicDetailViewModel *data2 = [[SEGWinningPublicDetailViewModel alloc] init];
        data2.detailsText = self.viewModel.winningPublicModel.noticeDetails;
        
        // 控件内容布局计算
        SEGWinningPublicDetailViewLayout *layout2 = [[SEGWinningPublicDetailViewLayout alloc]initWithData:data2 type:SEGEnumWinningPublicDetailViewType_NO];
        // 内容布局填充到视图
        SEGWinningPublicDetailView *varWinningPublicDetailView = [[SEGWinningPublicDetailView alloc]initWithFrame:CGRectZero];
        varWinningPublicDetailView.delegate = self;
        varWinningPublicDetailView.left = 0;
        varWinningPublicDetailView.top = varWinningPublicTopView.layout.totalHeight + 10;
        [varWinningPublicDetailView setWithLayout:layout2];
        self.detailView = varWinningPublicDetailView;
        [self.scrollView addSubview:varWinningPublicDetailView];
        
        self.scrollView.contentSize = CGSizeMake(kScreenWidth, varWinningPublicTopView.layout.totalHeight + 10 + varWinningPublicDetailView.layout.totalHeight);
        [self seg_initBottomToolView];
    } error:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}
- (void)seg_initBottomToolView {
    if (self.publicType == SEGWinningPublicType_Integral) {//积分商城类型的，回顾活动、去领奖的按钮都不要
        self.scrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
        return;
    }
    //底部控件
    // 控件数据模型创建
    SEGBottomToolViewModel *data = [[SEGBottomToolViewModel alloc]init];
    if (self.publicType == SEGWinningPublicType_Topic) {//话题没有去领奖
        //话题
        data.text1 = @"回顾活动";
    } else if (self.publicType == SEGWinningPublicType_Activity) {
        //活动
        data.text1 = @"回顾活动";
        data.text2 = @"去领奖";
    } 
    // 控件内容布局计算
    SEGBottomToolViewLayout *layout = [[SEGBottomToolViewLayout alloc]initWithData:data type:SEGEnumBottomToolViewType_NO];
    // 内容布局填充到视图
    SEGBottomToolView *varBottomToolView = [[SEGBottomToolView alloc]initWithFrame:CGRectZero];
    varBottomToolView.delegate = self;
    varBottomToolView.left = 0;
    varBottomToolView.top = CGRectGetHeight(self.view.frame)-bottomViewHeight-LYSafeAreaBottom();
    [varBottomToolView setWithLayout:layout];
    [self.view addSubview:varBottomToolView];
}

#pragma mark - Event
- (void)seg_backBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    if (scrollView.contentOffset.y > LYScreenHeight() * 0.25) {
        [self.naviTitle setHidden:NO];
        return;
    }
    [self.naviTitle setHidden:YES];
}

#pragma mark - SEGMessageNaviTitleNomalViewDelegate代理
- (void)naviTitleNomalViewBackButtonPressed {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - SEGprotocolWinningPublicDetailViewDelegate
- (void)changeHeightTo:(CGFloat)height {
    self.detailView.height = height;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, self.topView.layout.totalHeight + 10 + height);
}

#pragma mark - SEGprotocolBottomToolViewDelegate代理
- (void)SEGprotocollabel1:(SEGLabel *)sender {
    //回顾活动
    if (self.publicType == SEGWinningPublicType_Topic) {
        //话题
        NSDictionary *params = @{@"objId":self.viewModel.winningPublicModel.relationId};
        UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initTopicDetailViewController:params];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (self.publicType == SEGWinningPublicType_Activity) {
        //活动
        NSDictionary *params = @{@"actId":self.viewModel.winningPublicModel.relationId,@"nav":self.navigationController};
        [[SEGUhomeMediator sharedInstance] seg_goToActivityManageDetailViewController:params];
    }
}
- (void)SEGprotocollabel2:(SEGLabel *)sender {
    //去领奖
    BOOL isWinning = [self.viewModel.winningPublicModel.isWinning isEqualToString:@"1"];//中奖状态(1:已中奖,2:未中奖)
    if (isWinning) {
        NSDictionary *params = @{@"winnerId":self.viewModel.winningPublicModel.winningRecordId};
        UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initWinRecordDetailViewController:params];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        [SEGAlertView showTmepMessageWithMessage:@"很遗憾，您与奖品插肩而过，下次继续努力！"];
    }
}

#pragma mark - Getter
- (SEGMessageNaviTitleNomalView *)naviTitle {
    if (!_naviTitle) {
        _naviTitle = [[SEGMessageNaviTitleNomalView alloc] initWithFrame:CGRectZero];
        _naviTitle.btnDelegate = self;
        _naviTitle.hidden = YES;
    }
    return _naviTitle;
}
- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_header_back")] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(seg_backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-bottomViewHeight - LYSafeAreaBottom() + 0.5)];//+0.5为了最后一根分割线和底部工具栏分割线重合
        _scrollView.backgroundColor = kColor_bg;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (SEGWinningPublicViewModel *)viewModel {
    if (!_viewModel) _viewModel = [SEGWinningPublicViewModel new];
    return _viewModel;
}

@end



